package com.songcha.library_common.ui.view

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.LinearLayout
import com.songcha.library_common.R
import com.songcha.library_common.util.dp2px


class RatingView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet?=null,
    defStyle: Int = 0,
    defRes: Int = 0
) : LinearLayout(context,attrs,defStyle){
    companion object{
        const val SCORE_COUNT=5
    }

    private var mScoreSize=24f.dp2px()
    //rating
    private var mScoreNum=1
    private var mCanEvaluate=false
    private var mUnStarBorder=false
    private var mStarResId = R.mipmap.common_icon_star
    private var mUnStarResId = R.mipmap.common_icon_unstar
    private var mUnStarBorderResId = R.mipmap.common_icon_unstar_border
    private var mGapRatio = 0.25f
    private var mMaxScore = 5

    init {
        initView(context,attrs)
    }

    constructor(context:Context,size:Float,score:Int,unStarBorder:Boolean = false) : this(context) {
        mScoreSize = size
        mScoreNum = score
        mUnStarBorder = unStarBorder
        initImage()
    }

    private fun initView(context:Context,attrs:AttributeSet?){
        orientation=HORIZONTAL

        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.RatingView)
            mScoreSize=ta.getDimension(R.styleable.RatingView_rating_size,24f.dp2px())
            mScoreNum=ta.getInt(R.styleable.RatingView_rating_num,1)
            mCanEvaluate=ta.getBoolean(R.styleable.RatingView_rating_can_evaluate,false)
            mUnStarBorder=ta.getBoolean(R.styleable.RatingView_rating_unstar_border,false)
            mMaxScore=ta.getInt(R.styleable.RatingView_rating_max_score,5)
            if(mScoreNum<1) mScoreNum=1
            if(mScoreNum>mMaxScore) mScoreNum=mMaxScore
            ta.recycle()
        }


        for(i in 0 until SCORE_COUNT){
            val view=ImageView(context)
            view.setImageResource(R.mipmap.common_icon_unstar)
            view.scaleType=ImageView.ScaleType.CENTER_CROP
            val lp=LayoutParams(mScoreSize.toInt(),mScoreSize.toInt())
            if(i<SCORE_COUNT-1)
                lp.rightMargin=(mScoreSize/4).toInt()
            view.layoutParams=lp
            if(mCanEvaluate){
                view.setOnClickListener {
                    setScore(i+1)
                }
            }

            addView(view)
        }
    }

    fun setImageRes(starId:Int,unStarId:Int,unStarBorderId:Int?=null){
        mStarResId = starId
        mUnStarResId = unStarId
        if(unStarBorderId!=null) mUnStarBorderResId = unStarBorderId
        initImage()
    }

    fun setGapRatio(ratio:Float){
        mGapRatio = ratio
        initImage()
    }

    private fun initImage(){
        removeAllViews()

        for(i in 0 until SCORE_COUNT){
            val view=ImageView(context)
            view.setImageResource(mUnStarResId)
            view.scaleType=ImageView.ScaleType.CENTER_CROP
            val lp = LayoutParams(mScoreSize.toInt(),mScoreSize.toInt())
            if(i < SCORE_COUNT - 1)
                lp.rightMargin=(mScoreSize * mGapRatio).toInt()
            view.layoutParams=lp
            if(mCanEvaluate){
                view.setOnClickListener {
                    setScore(i+1)
                }
            }

            addView(view)
        }
    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        updateScore()
    }

    fun setScore(score:Int){
        var tmp=score
        if(tmp<0) tmp=0
        if(tmp>mMaxScore) tmp=mMaxScore
        if(tmp==mScoreNum) return
        mScoreNum=score
        updateScore()
    }

    fun getScore():Int{
        return mScoreNum
    }

    private fun updateScore(){
        if(childCount!=SCORE_COUNT) return
        if(mScoreNum<0) mScoreNum=0
        if(mScoreNum>mMaxScore) mScoreNum=mMaxScore
        val rating = mScoreNum/(mMaxScore/SCORE_COUNT)
        for(i in 0 until childCount){
            val item=getChildAt(i) as ImageView
            if(rating>=i+1){
                item.setImageResource(mStarResId)
            }else{
                if(mUnStarBorder){
                    item.setImageResource(mUnStarBorderResId)
                }else{
                    item.setImageResource(mUnStarResId)
                }

            }
        }


    }



}